import 'package:equatable/equatable.dart';
import '../entities/anime.dart';

/// 收藏实体类
class Favorite extends Equatable {
  final String id;
  final int animeId;
  final Anime anime;
  final FavoriteStatus status;
  final DateTime addedDate;
  final int? watchedEpisodes;
  final double? personalScore;
  final String? notes;

  const Favorite({
    required this.id,
    required this.animeId,
    required this.anime,
    required this.status,
    required this.addedDate,
    this.watchedEpisodes,
    this.personalScore,
    this.notes,
  });

  @override
  List<Object?> get props => [
        id,
        animeId,
        anime,
        status,
        addedDate,
        watchedEpisodes,
        personalScore,
        notes,
      ];

  /// 获取状态文本
  String get statusText {
    switch (status) {
      case FavoriteStatus.wantToWatch:
        return '想看';
      case FavoriteStatus.watching:
        return '在看';
      case FavoriteStatus.completed:
        return '已看完';
      case FavoriteStatus.onHold:
        return '暂停';
      case FavoriteStatus.dropped:
        return '弃番';
    }
  }

  /// 获取观看进度文本
  String get progressText {
    if (watchedEpisodes == null) return '';
    
    final total = anime.episodes;
    if (total != null) {
      return '$watchedEpisodes/$total';
    } else {
      return '$watchedEpisodes话';
    }
  }

  /// 获取完成度百分比
  double get progressPercentage {
    if (watchedEpisodes == null || anime.episodes == null) return 0.0;
    return (watchedEpisodes! / anime.episodes!).clamp(0.0, 1.0);
  }

  /// 判断是否已完成
  bool get isCompleted {
    return status == FavoriteStatus.completed;
  }

  /// 判断是否正在观看
  bool get isWatching {
    return status == FavoriteStatus.watching;
  }

  /// 复制并更新
  Favorite copyWith({
    String? id,
    int? animeId,
    Anime? anime,
    FavoriteStatus? status,
    DateTime? addedDate,
    int? watchedEpisodes,
    double? personalScore,
    String? notes,
  }) {
    return Favorite(
      id: id ?? this.id,
      animeId: animeId ?? this.animeId,
      anime: anime ?? this.anime,
      status: status ?? this.status,
      addedDate: addedDate ?? this.addedDate,
      watchedEpisodes: watchedEpisodes ?? this.watchedEpisodes,
      personalScore: personalScore ?? this.personalScore,
      notes: notes ?? this.notes,
    );
  }
}

/// 收藏状态枚举
enum FavoriteStatus {
  wantToWatch,  // 想看
  watching,     // 在看
  completed,    // 已看完
  onHold,       // 暂停
  dropped,      // 弃番
}

/// 收藏状态扩展
extension FavoriteStatusExtension on FavoriteStatus {
  String get displayName {
    switch (this) {
      case FavoriteStatus.wantToWatch:
        return '想看';
      case FavoriteStatus.watching:
        return '在看';
      case FavoriteStatus.completed:
        return '已看完';
      case FavoriteStatus.onHold:
        return '暂停';
      case FavoriteStatus.dropped:
        return '弃番';
    }
  }

  String get key {
    switch (this) {
      case FavoriteStatus.wantToWatch:
        return 'want_to_watch';
      case FavoriteStatus.watching:
        return 'watching';
      case FavoriteStatus.completed:
        return 'completed';
      case FavoriteStatus.onHold:
        return 'on_hold';
      case FavoriteStatus.dropped:
        return 'dropped';
    }
  }

  static FavoriteStatus fromKey(String key) {
    switch (key) {
      case 'want_to_watch':
        return FavoriteStatus.wantToWatch;
      case 'watching':
        return FavoriteStatus.watching;
      case 'completed':
        return FavoriteStatus.completed;
      case 'on_hold':
        return FavoriteStatus.onHold;
      case 'dropped':
        return FavoriteStatus.dropped;
      default:
        return FavoriteStatus.wantToWatch;
    }
  }
} 